<?php

class ShareModel
{
    private $shareKeyPrefix =   'SI/';
    private $shareMeType    =   1;
    private $meShareType    =   2;

    public function getShareMeList($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'SIM007';
            return  FALSE;
        }

        $start  =   0;
        $length =   AC::SHARE_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   array('shareType' => $this->shareMeType);
        if (!empty($info['id']))    $where  =   ' id > '. $info['id']. ' AND `shareType` = '. $this->shareMeType. ' ';
        $orderBy=   ' id DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/
    public function getShareMeListUp($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'SIM007';
            return  FALSE;
        }
        if (empty($info['id'])) return  $this->getShareMeList($info);

        $start  =   0;
        $length =   AC::SHARE_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   ' id > '. $info['id']. ' AND `shareType` = '. $this->shareMeType. ' ';
        $orderBy=   ' id ASC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/
    public function getShareMeListDown($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'SIM007';
            return  FALSE;
        }
        if (empty($info['id'])) return  $this->getShareMeList($info);

        $start  =   0;
        $length =   AC::SHARE_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   array('shareType' => $this->shareMeType);
        $where  =   ' id < '. $info['id']. ' AND `shareType` = '. $this->shareMeType. ' ';
        $orderBy=   ' id DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/

    public function getMeShareList($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'SIM006';
            return  FALSE;
        }

        $start  =   0;
        $length =   AC::SHARE_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   array('shareType' => $this->meShareType);
        if (!empty($info['id']))    $where  =   ' id > '. $info['id']. ' AND `shareType` = '. $this->meShareType. ' ';
        $orderBy=   ' id DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/
    public function getMeShareListUp($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'SIM006';
            return  FALSE;
        }

        $start  =   0;
        $length =   AC::SHARE_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   ' id > '. $info['id']. ' AND `shareType` = '. $this->meShareType. ' ';
        $orderBy=   ' id ASC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/
    public function getMeShareListDown($info)/*{{{*/
    {
        if (empty($info['uid']))
        {
            AC::$errorNo    =   'SIM006';
            return  FALSE;
        }

        $start  =   0;
        $length =   AC::SHARE_LIST_DISPLAY_LEN;
        isset($info['start'])   &&  $start  =   $info['start'];
        isset($info['length'])  &&  $length =   $info['length'];

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   ' id < '. $info['id']. ' AND `shareType` = '. $this->meShareType. ' ';
        $orderBy=   ' id DESC ';
        $result =   Storage::getConditionManyData($key, $where, $orderBy, $start, $length);
        return  $result;
    }/*}}}*/

    public function postShare($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['nickname'])||  empty($info['sUid'])    ||  empty($info['sNickname']))
        {
            AC::$errorNo    =   'SIM001';
            return  FALSE;
        }
        if (empty($info['type'])||  empty($info['content']) ||  empty($info['bodyId']))
        {
            AC::$errorNo    =   'SIM002';
            return  FALSE;
        }

        if (AC::FEED_SHARE  ==  $info['type'])
        {
            AC::$errorNo    =   'SIM008';
            return  FALSE;
        }

        #   post MeShare    /*{{{*/
        $dataMeShare    =   array(
                                'uid'       =>  $info['uid'],
                                'sUid'      =>  $info['sUid'],
                                'sNickname' =>  $info['sNickname'],
                                'bodyId'    =>  $info['bodyId'],
                                'type'      =>  $info['type'],
                                'content'   =>  $info['content'],
                                'shareType' =>  $this->meShareType,
                                'time'      =>  AC::SERVER_TIME,
                            );
        $meShareKey     =   $this->shareKeyPrefix. $info['uid'];
        $meShareResult  =   Storage::addData($meShareKey, $dataMeShare);
        if (empty($meShareResult))
        {
            AC::$errorNo    =   'SIM003';
            return  FALSE;
        }/*}}}*/

        #   post ShareMe    /*{{{*/
        $dataShareMe    =   array(
                                'uid'       =>  $info['sUid'],
                                'sUid'      =>  $info['uid'],
                                'sNickname' =>  $info['nickname'],
                                'bodyId'    =>  $info['bodyId'],
                                'type'      =>  $info['type'],
                                'content'   =>  $info['content'],
                                'shareType' =>  $this->shareMeType,
                                'time'      =>  AC::SERVER_TIME,
                            );
        $shareMeKey     =   $this->shareKeyPrefix. $info['sUid'];
        $shareMeResult  =   Storage::addData($shareMeKey, $dataShareMe);
        if (empty($shareMeResult))
        {
            AC::$errorNo    =   'SIM004';
            return  FALSE;
        }/*}}}*/

        return  TRUE;
    }/*}}}*/

    public function removeShare($info)/*{{{*/
    {
        if (empty($info['uid']) ||  empty($info['id']))
        {
            AC::$errorNo    =   'SIM005';
            return  FALSE;
        }

        $key    =   $this->shareKeyPrefix. $info['uid'];
        $where  =   array('id' => $info['id']);
        $result =   Storage::delConditionData($key, $where);

        return  $result;
    }/*}}}*/
    
}


